#include <head.h>


int main(int argc, char const *argv[])
{
    int pipe_1[2];

    char buf[1024];
    pid_t pid;
    if (-1 == pipe(pipe_1))
    {
        perror("pipe");
        exit(1);
    }
    if (-1 ==(pid = fork()))
    {
        ERRLOG("fork");
        return 1;
    }else if (0 == pid)
    {
        memset(buf, 0, sizeof(buf));
        close(pipe_1[1]);
        while(1){
            read(pipe_1[0],buf,sizeof(buf));
            printf("接收到了：%s\n",buf);
            if (!strcmp(buf,"quit"))
            {
                close(pipe_1[0]);
                exit(1);
            }
            memset(buf ,0,sizeof(buf));
            
        }
    }else if (pid > 0)
    {
        close(pipe_1[0]);
        while(1){
            printf("请输入：");
            scanf("%s",buf);
            getchar();
            buf[strlen(buf)] = '\0';
            if (!strcmp(buf,"quit"))
            {
                write(pipe_1[1],buf,strlen(buf));
                close(pipe_1[1]);
                wait(NULL);
                exit(1);
            }
            write(pipe_1[1],buf,strlen(buf));
            memset(buf,0,sizeof(buf));
        }
    }
    return 0;
}